package com.dingtalk.util;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiChatSendRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiChatSendResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DingTalkUtil {

    public static void sendGroupMessage(String chatId, String text, String accessToken) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/chat/send");
        OapiChatSendRequest request = new OapiChatSendRequest();
        request.setChatid(chatId);
        request.setMsgtype("text");

        OapiChatSendRequest.Msg msg = new OapiChatSendRequest.Msg();
        msg.setText(new OapiChatSendRequest.Text());
        msg.getText().setContent(text);
        request.setMsg(msg);

        OapiChatSendResponse response = client.execute(request, accessToken);
        System.out.println("发送结果: " + response.getBody());
    }

    public static String getAccessToken(String appKey, String appSecret) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appKey);
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");

        OapiGettokenResponse response = client.execute(request);
        return response.getAccessToken();
    }
} 